package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.common.util.AlipayAccountCheckUtil;
import com.net.common.util.MoneyUtil;
import com.net.common.util.PhoneUtil;
import com.net.common.web.CookieHelper;
import com.net.xpay.admin.po.UpdatePartnerPo;
import com.net.xpay.admin.secutiry.SessionManager;
import com.net.xpay.admin.vo.PartnerCodeVo;
import com.net.xpay.admin.vo.PartnerInfoVo;
import com.net.xpay.admin.vo.PartnerRegisterVo;
import com.net.xpay.admin.vo.PartnerVo;
import com.net.xpay.common.annotation.OperateLog;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.partner.PartnerAccount;
import com.net.xpay.common.domain.partner.PartnerAccountLog;
import com.net.xpay.common.domain.partner.PartnerCode;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.enums.config.FunctionModule;
import com.net.xpay.common.enums.config.MethodType;
import com.net.xpay.common.enums.partner.PartnerLevel;
import com.net.xpay.common.enums.user.ClientFrom;
import com.net.xpay.common.enums.user.CommissionMode;
import com.net.xpay.common.enums.user.ServicePayMode;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.partner.PartnerAccountLogManager;
import com.net.xpay.common.manager.partner.PartnerAccountManager;
import com.net.xpay.common.manager.partner.PartnerCodeManager;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.model.GetPartnerInfoModel;
import com.net.xpay.common.model.PartnerRegisterModel;
import com.net.xpay.common.model.search.PartnerSearchModel;
import com.net.xpay.common.service.PartnerService;
import com.net.xpay.core.constant.CoreConstant;
import com.net.xpay.core.constant.PartnerWebConstant;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.enums.AccountBillType;
import com.net.xpay.core.helper.DomainHelper;
import com.net.xpay.core.model.IdModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 2019/4/1 14:45

 */
@Slf4j
@RestController
@RequestMapping("/admin/partner")
@Api(tags = "运营/合伙人管理", description = "运营 合伙人管理 net")
public class AdminPartnerController {

    @Autowired
    private PartnerManager partnerManager;

    @Autowired
    private PartnerService partnerService;

    @Autowired
    private PartnerAccountManager partnerAccountManager;

    @Autowired
    private PartnerCodeManager partnerCodeManager;

    @Autowired
    private DomainHelper domainHelper;

    @Autowired
    private SessionManager sessionManager;

    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;

    @Autowired
    private PartnerAccountLogManager partnerAccountLogManager;

    @Autowired
    private UserManager userManager;

    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::search')")
    public ResultPagedList<PartnerVo> searchPartner(@RequestBody PartnerSearchModel searchModel) {
        List<Partner> records = partnerManager.search(searchModel);
        long count = partnerManager.searchCount(searchModel);

        List<PartnerVo> vos = BeanCopierUtil.batchCopy(Partner.class, PartnerVo.class, records);
        for (PartnerVo vo : vos) {
            List<PartnerCode> partnerCodes = partnerCodeManager.listByPartnerId(vo.getId());
            List<PartnerCodeVo> partnerCodeVos = BeanCopierUtil.batchCopy(PartnerCode.class, PartnerCodeVo.class, partnerCodes);
            for (PartnerCodeVo partnerCodeVo : partnerCodeVos) {
                partnerCodeVo.setName(partnerCodeVo.getUserTag().getDesc());
            }
            vo.setPartnerCodes(partnerCodeVos);
            PartnerAccount account = partnerAccountManager.getById(vo.getId());
            vo.setMoneyLeft(account.getMoneyLeft());
        }

        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("注册合伙人")
    @PostMapping("/register")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::register')")
    @OperateLog(methodType = MethodType.INSERT, methodDesc = "注册合伙人",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<PartnerRegisterVo> register(@RequestBody PartnerRegisterModel registerModel) {
        //验证手机号
        if (!PhoneUtil.isEffectiveMobile(registerModel.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
        //确认手机号未被注册
        if (partnerManager.findByMobile(registerModel.getMobile()) != null) {
            throw new BusinessException(PoseidonErrorCode.MOBILE_DUPLICATED, "手机号已经被注册,你可以使用找回密码功能");
        }

        String randomPassword = RandomStringUtils.randomAlphabetic(3).toLowerCase() + "@" + Strings.padStart("" + RandomUtils.nextInt(0, 1000000), 6, '0');
        registerModel.setPassword(randomPassword);
        registerModel.setClientFrom(ClientFrom.PC_WEB);
        Partner partner = partnerService.registerInternal(registerModel);
        PartnerRegisterVo vo = new PartnerRegisterVo();
        vo.setMobile(partner.getMobile());
        vo.setPassword(randomPassword);
        return new ResultData<>(vo);
    }

    /**
     * 一键登录
     */
    @GetMapping("/login")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @OperateLog(methodType = MethodType.INSERT, methodDesc = "登录合伙人后台",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<String> loginUser(HttpServletResponse response, HttpServletRequest request, Long userId) {
        Preconditions.checkArgument(userId != null);

        String cookieDomain = domainHelper.getCookieDomain(request);
        cookieDomain = cookieDomain.substring(cookieDomain.indexOf(".") + 1);

        //把之前的session先给删除了
        CookieHelper.deleteCookie(response, cookieDomain, "/", PartnerWebConstant.SESSION_COOKIE_NAME);

        String key = sessionManager.getRememberMeKey(userId, ClientFrom.PC_WEB);
        Cookie rememberCookie = CookieHelper.createCookie("/", PartnerWebConstant.REMEMBER_ME_COOKIE_NAME, key, PartnerWebConstant.REMEMBER_ME_COOKIE_MAX_AGE, null);
        CookieHelper.setCookie(response, cookieDomain, rememberCookie);
        String partnerDomain = commonSysConfigHelper.getString(CommonSysConfigKey.PARTNER_DOMAIN);
        return new ResultData<>(partnerDomain);
    }

    @ApiOperation("设置合伙人分佣")
    @PostMapping("/update-benefit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::update-benefit')")
    @OperateLog(methodType = MethodType.UPDATE, methodDesc = "设置合伙人分佣",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<Boolean> updateBenefit(@RequestBody UpdatePartnerPo po) {
        Preconditions.checkArgument(po.getPartnerCodes() != null);

        for (PartnerCode code : po.getPartnerCodes()) {
            if (code.getMode() == ServicePayMode.FIXED_RATIO && CommissionMode.COST_MODE.equals(code.getCommissionMode())) {
                if (code.getCostServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "平台收取合伙人成本费率不能为空");
                }
                if (code.getCostServiceRatio().compareTo(CoreConstant.DEFAULT_PARTNER_COST_SERVICE_UNIT_RATIO) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "平台收取合伙人成本费率不能低于" + CoreConstant.DEFAULT_PARTNER_COST_SERVICE_UNIT_RATIO);
                }
                if (code.getSafePayBankCostServiceRatio().compareTo(CoreConstant.DEFAULT_PARTNER_SAFE_PAY_BANK_COST_SERVICE_UNIT_RATIO) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "平台收取合伙人安全付到卡成本费率不能低于" + CoreConstant.DEFAULT_PARTNER_COST_SERVICE_UNIT_RATIO);
                }
                if (code.getCostServiceRatio().compareTo(new BigDecimal(1)) > 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "平台收取合伙人成本费率不能高于1");
                }
                if (code.getServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人收取用户默认费率不能为空");
                }
                if (code.getCostServiceRatio().compareTo(code.getServiceRatio()) > 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "平台成本不能高于收取的用户费率");
                }
            }
            partnerCodeManager.updateByPrimaryKeyWithVersion(code);
        }
        return new ResultData<>(true);
    }

    @ApiOperation("设置二级合伙人分佣")
    @PostMapping("/update-level2-benefit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::update-level2-benefit')")
    @OperateLog(methodType = MethodType.UPDATE, methodDesc = "设置二级合伙人分佣",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<Boolean> updateLevel2Benefit(@RequestBody UpdatePartnerPo po) {
        Preconditions.checkArgument(po.getPartnerCodes() != null);

        Partner partner = partnerManager.getById(po.getId());
        if (partner.getLevel() != PartnerLevel.LEVEL_2) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "只能设置二级合伙人分佣");
        }

        for (PartnerCode code : po.getPartnerCodes()) {
            if (code.getMode() != ServicePayMode.FIXED_RATIO) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "只能选择固定费率模式收费");
            }
            if (code.getCommissionMode() == null || CommissionMode.STAT_MODE == code.getCommissionMode()) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣模式设置错误");
            }

            if (code.getServiceRatio() == null) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人旗下商户到户默认费率不能为空");
            }

            if (code.getServiceRatio().compareTo(new BigDecimal("0.002")) < 0) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人旗下商户到户默认费率不能小于0.002");
            }

            if (code.getBankServiceRatio() == null) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人旗下商户到卡默认费率不能为空");
            }

            if (code.getBankServiceRatio().compareTo(new BigDecimal("0.002")) < 0) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人旗下商户到卡默认费率不能小于0.002");
            }

            if (code.getCommissionMode() == CommissionMode.FIXED_MODE) {
                if (code.getBenefitRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣比例不能为空");
                }
                if (code.getBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣比例不能大于0.8");
                }
                if (code.getBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣比例不能小于0");
                }
                if (code.getFatherBenefitRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能为空");
                }
                if (code.getFatherBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能大于0.8");
                }
                if (code.getFatherBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能小于0");
                }

                code.setFatherCommissionMode(CommissionMode.FIXED_MODE);
            } else if (code.getCommissionMode() == CommissionMode.COST_MODE) {
                if (code.getCostServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能为空");
                }

                if (code.getCostServiceRatio().compareTo(new BigDecimal("0.001")) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能低于0.001");
                }

                if (code.getCostServiceRatio().compareTo(new BigDecimal("1")) >= 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能高于1");
                }
                if (code.getCostServiceRatio().compareTo(code.getServiceRatio()) >= 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本必须小于到户费率");
                }

                if (code.getSafePayBankCostServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能为空");
                }

                if (code.getSafePayBankCostServiceRatio().compareTo(new BigDecimal("0.001")) < 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能低于0.001");
                }

                if (code.getSafePayBankCostServiceRatio().compareTo(new BigDecimal("1")) >= 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能高于1");
                }

                if (code.getSafePayBankCostServiceRatio().compareTo(code.getBankServiceRatio()) >= 0) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本必须小于到卡费率");
                }


                if (code.getFatherCommissionMode() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择上级模式");
                }

                if (code.getFatherCommissionMode() == CommissionMode.FIXED_MODE) {
                    if (code.getFatherBenefitRatio() == null) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能为空");
                    }
                    if (code.getFatherBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能大于0.8");
                    }
                    if (code.getFatherBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能小于0");
                    }
                } else if (code.getFatherCommissionMode() == CommissionMode.COST_MODE) {
                    if (code.getFatherServiceRatio() == null) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户上级合伙人分佣费率不能为空");
                    }
                    if (code.getCostServiceRatio().subtract(code.getFatherServiceRatio()).compareTo(new BigDecimal("0.001")) < 0) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本-到户上级合伙人分佣费率必须大于等于0.001");
                    }
                    if (code.getFatherBankServiceRatio() == null) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡上级合伙人分佣费率不能为空");
                    }
                    if (code.getSafePayBankCostServiceRatio().subtract(code.getFatherBankServiceRatio()).compareTo(new BigDecimal("0.001")) < 0) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本-到银行卡上级合伙人分佣费率必须大于等于0.001");
                    }
                }
            }

            partnerCodeManager.updateByPrimaryKeyWithVersion(code);
        }
        return new ResultData<>(true);
    }

    @ApiOperation("修改合伙人信息")
    @PostMapping("/update-partner-info")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::update-partner-info')")
    @OperateLog(methodType = MethodType.UPDATE, methodDesc = "修改合伙人信息",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<Partner> updatePartnerInfo(@RequestBody UpdatePartnerPo po) {
        Preconditions.checkArgument(po.getId() != null);
        Partner partner = partnerManager.getById(po.getId());
        partner.setName(po.getName());
        partner.setRemark(po.getRemark());
        if (po.getSettlePeriod() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人结算周期不能为空");
        }
        partner.setSettlePeriod(po.getSettlePeriod());
        if (po.getAutoWithdraw() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人余额是否自动提现不能为空");
        }
        if (po.getAutoWithdraw()) {
            String partnerWithdrawSafePayAccount = commonSysConfigHelper.getString(CommonSysConfigKey.PARTNER_WITHDRAW_SAFE_PAY_ACCOUNT);
            String partnerWithdrawMobile = commonSysConfigHelper.getString(CommonSysConfigKey.PARTNER_WITHDRAW_MOBILE);
            if (Strings.isNullOrEmpty(partnerWithdrawSafePayAccount) || Strings.isNullOrEmpty(partnerWithdrawMobile)) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "未配置安全发打款企业支付宝账号，请联系技术配置");
            }
        }
        if (Boolean.TRUE.equals(po.getAutoWithdraw())) {
            if (Strings.isNullOrEmpty(po.getAlipayName())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "选择了余额自动提现，提现支付宝实名不能为空");
            }
            if (Strings.isNullOrEmpty(po.getAlipayAccount())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "选择了余额自动提现，提现支付宝账号不能为空");
            }
        }
        if (!Strings.isNullOrEmpty(po.getAlipayAccount())) {
            if (!AlipayAccountCheckUtil.isEffectiveAccount(po.getAlipayAccount())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "提现支付宝账号格式不正确");
            }
        }

        if (partner.getLevel() == PartnerLevel.LEVEL_2) {
            if (po.getFatherPartnerId() == null) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择上级合伙人");
            }
            partner.setFatherPartnerId(po.getFatherPartnerId());
            //需要把二级下面的商户的一级设置为新的一级账号
            List<User> users = userManager.listByPartnerId(partner.getId());
            for (User user : users) {
                user.setFatherPartnerId(po.getFatherPartnerId());
                userManager.updateByPrimaryKeyWithVersion(user);
            }
        }

        partner.setAutoWithdraw(po.getAutoWithdraw());
        partner.setWechatQrCode(po.getWechatQrCode());
        partner.setAlipayAccount(po.getAlipayAccount());
        partner.setAlipayName(po.getAlipayName());
        partnerManager.updateByPrimaryKeyWithVersion(partner);
        return new ResultData<>(partner);
    }

    @ApiOperation("修改合伙人状态")
    @PostMapping("/update-partner-security-status")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::update-partner-security-status')")
    @OperateLog(methodType = MethodType.UPDATE, methodDesc = "修改合伙人状态",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<Partner> updatePartnerSecurityStatus(@RequestBody UpdatePartnerPo po) {
        Preconditions.checkArgument(po.getId() != null);
        Partner partner = partnerManager.getById(po.getId());
        partner.setSecurityStatus(po.getSecurityStatus());
        partner.setSecurityRemark(po.getSecurityRemark());
        partnerManager.updateByPrimaryKeyWithVersion(partner);
        return new ResultData<>(partner);
    }

    @ApiOperation("修改合伙人密码")
    @PostMapping("/reset-partner-password")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::reset-partner-password')")
    @OperateLog(methodType = MethodType.UPDATE, methodDesc = "修改合伙人密码",module = FunctionModule.PARTNER_MANAGE)
    public ResultData<String> resetPartnerPassword(@RequestBody IdModel model) {
        Preconditions.checkArgument(model.getId() != null);
        Partner partner = partnerManager.getById(model.getId());
        String psw = RandomStringUtils.randomAlphabetic(3).toLowerCase() + "@" + Strings.padStart("" + RandomUtils.nextInt(0, 1000000), 6, '0');
        partner.setPassword(BCrypt.hashpw(psw, BCrypt.gensalt()));
        partnerManager.updateByPrimaryKeyWithVersion(partner);
        return new ResultData<>(psw);
    }


    @ApiOperation("查询合伙人信息（姓名，手机号,编号）")
    @PostMapping("/get-info")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner::get-info')")
    public ResultList<PartnerInfoVo> getInfo(@RequestBody GetPartnerInfoModel model) {
        List<Partner> list = partnerManager.infoList(model);
        List<PartnerInfoVo> vos = BeanCopierUtil.batchCopy(Partner.class, PartnerInfoVo.class, list);
        List<PartnerInfoVo> records = new ArrayList<>();
        for (PartnerInfoVo vo : vos) {
            if (Boolean.TRUE.equals(model.getExcludeStatModePartner())) {
                if (vo.getLevel() == PartnerLevel.LEVEL_1) {
                    List<PartnerCode> partnerCodes = partnerCodeManager.listByPartnerId(vo.getId());
                    if (partnerCodes.get(0).getCommissionMode() == CommissionMode.STAT_MODE) {
                        continue;
                    }
                }
            }

            vo.setShowContent(vo.getId() + "-" + vo.getName() + "-" + vo.getMobile());
            records.add(vo);
        }
        return new ResultList<>(records);
    }

    @ApiOperation("搜索合伙人上月结算金额")
    @PostMapping("/get-last-month-settle-benefit")
    @ResponseBody
    public ResultData<String> getLastMonthSettleBenefit(@RequestBody IdModel idModel) {
        PartnerAccountLog log1 = partnerAccountLogManager.findLastByType(idModel.getId(), AccountBillType.PARTNER_BENEFIT_SETTLE);
        PartnerAccountLog log2 = partnerAccountLogManager.findLastByType(idModel.getId(), AccountBillType.PARTNER_SAFE_PAY_BENEFIT_SETTLE);

        BigDecimal benefit = new BigDecimal("0.00");
        if (log1 != null) {
            benefit = benefit.add(MoneyUtil.convertCentToBigDecimal(log1.getChange(), 2));
        }
        if (log2 != null) {
            benefit = benefit.add(MoneyUtil.convertCentToBigDecimal(log2.getChange(), 2));
        }

        return new ResultData<>(benefit.toString());
    }


}
